package com.example.springbooti18.controller;

import com.example.springbooti18.entity.Message;
import com.example.springbooti18.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 国际化消息管理控制器
 * 提供REST API接口用于管理数据库中的国际化消息
 */
@RestController
@RequestMapping("/api/messages")
public class MessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 获取所有消息
     */
    @GetMapping
    public ResponseEntity<List<Message>> getAllMessages() {
        List<Message> messages = messageService.getAllMessages();
        return ResponseEntity.ok(messages);
    }

    /**
     * 根据消息键和语言代码获取消息
     */
    @GetMapping("/key/{key}/lang/{lang}")
    public ResponseEntity<Message> getMessageByKeyAndLang(@PathVariable String key, @PathVariable String lang) {
        Optional<Message> message = messageService.getMessage(key, lang);
        return message.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
    }

    /**
     * 获取指定语言的所有消息
     */
    @GetMapping("/lang/{lang}")
    public ResponseEntity<List<Message>> getMessagesByLang(@PathVariable String lang) {
        List<Message> messages = messageService.getMessagesByLanguageCode(lang);
        return ResponseEntity.ok(messages);
    }

    /**
     * 获取指定消息键的所有语言版本
     */
    @GetMapping("/key/{key}")
    public ResponseEntity<List<Message>> getMessagesByKey(@PathVariable String key) {
        List<Message> messages = messageService.getMessagesByKey(key);
        return ResponseEntity.ok(messages);
    }

    /**
     * 创建新消息
     */
    @PostMapping
    public ResponseEntity<Message> createMessage(@RequestBody Message message) {
        Message savedMessage = messageService.saveMessage(message);
        return ResponseEntity.status(HttpStatus.CREATED).body(savedMessage);
    }

    /**
     * 更新消息
     */
    @PutMapping
    public ResponseEntity<Message> updateMessage(@RequestBody Message message) {
        Message updatedMessage = messageService.saveMessage(message);
        return ResponseEntity.ok(updatedMessage);
    }

    /**
     * 删除消息
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteMessage(@PathVariable Long id) {
        messageService.deleteMessage(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 根据消息键和语言代码删除消息
     */
    @DeleteMapping("/key/{key}/lang/{lang}")
    public ResponseEntity<Void> deleteMessageByKeyAndLang(@PathVariable String key, @PathVariable String lang) {
        messageService.deleteMessage(key, lang);
        return ResponseEntity.noContent().build();
    }

    /**
     * 获取所有支持的语言代码
     */
    @GetMapping("/languages")
    public ResponseEntity<List<String>> getAllLanguageCodes() {
        List<String> languages = messageService.getAllLanguageCodes();
        return ResponseEntity.ok(languages);
    }

    /**
     * 检查消息是否存在
     */
    @GetMapping("/exists/key/{key}/lang/{lang}")
    public ResponseEntity<Map<String, Boolean>> existsMessage(@PathVariable String key, @PathVariable String lang) {
        boolean exists = messageService.existsMessage(key, lang);
        return ResponseEntity.ok(Map.of("exists", exists));
    }

    /**
     * 批量创建或更新消息
     */
    @PostMapping("/batch")
    public ResponseEntity<List<Message>> batchSaveMessages(@RequestBody List<Message> messages) {
        List<Message> savedMessages = messageService.saveMessages(messages);
        return ResponseEntity.status(HttpStatus.CREATED).body(savedMessages);
    }
}